﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class addjilu : System.Web.UI.Page
{
    Datacommand datacom = new Datacommand();
    seleData ddsj = new seleData();
    Datacon datacon = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["ID"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            Label1.Text = ddsj.chasheng(Session["diqu"].ToString()) + ddsj.chashi(Session["diqu"].ToString()) + ddsj.chaqu(Session["diqu"].ToString()) + ddsj.chadanwei(Session["juhao"].ToString());
            TextBox5.Text = ddsj.chaname(Session["diqu"].ToString(), Session["juhao"].ToString());
            TextBox6.Text = ddsj.chadianhua(Session["diqu"].ToString(), Session["juhao"].ToString());
            datacom.ecDropDownList(DropDownList3, "select * from qingkuangjilu", "qingkuang", "id");
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //先判断单位记录中是否有此数据
        SqlConnection cn = datacon.getcon();
        cn.Open();

        string sql = "select  id from danwei where  diquhao='" + Session["diqu"].ToString() + "' and juhao=" + Session["juhao"].ToString();
        SqlCommand myCommand = new SqlCommand(sql, cn);

        SqlDataReader reader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

        if (reader.Read())//如果存在，增加访问记录
        {
            datacom.eccom("insert into lianxijilu (danweiid,riqi,lxr,nr,dh,jbr,dqqk) values (" + reader["id"].ToString() + ",'" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox2.Text + "','" + TextBox6.Text + "','" + Session["name"] + "'," + DropDownList3.SelectedValue + ")");
            datacom.eccom("update danwei set zhuangtai=" + DropDownList3.SelectedValue + " where id=" + reader["id"].ToString());
            Label2.Text = "新增成功";
        }
        else
        {
            datacom.eccom("insert into danwei (juhao,diquhao,riqi,zhuangtai) values (" + Session["juhao"].ToString() + ",'" + Session["diqu"].ToString() + "',getdate()," + DropDownList3.SelectedValue + ")");
            myCommand.CommandText = "select id from danwei where  diquhao='" + Session["diqu"].ToString() + "' and juhao=" + Session["juhao"].ToString();
            reader.Close();
            cn.Close();
            cn.Open();
            string newid = myCommand.ExecuteScalar().ToString();
            datacom.eccom("insert into lianxijilu (danweiid,riqi,lxr,nr,dh,jbr,dqqk) values (" + newid + ",'" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox2.Text + "','" + TextBox6.Text + "','" + Session["name"] + "'," + DropDownList3.SelectedValue + ")");
            datacom.eccom("update danwei set zhuangtai=" + DropDownList3.SelectedValue + "where id=" + newid);
            Label2.Text = "新增成功";
        }
        cn.Close();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("guanliziliao.aspx");
    }
}